package com.example.springbeanlife;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @ClassName BeanPostProcessorConfig
 * @Description
 * @Author Yin
 * @Date 2021/4/8 13:48
 * @Version 1.0
 */
//@Component
public class BeanPostProcessorConfig implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("执行了BeanPostProcessor");
        Class<?> aClass = bean.getClass();
        for (Field declaredField : aClass.getDeclaredFields()) {
            if (declaredField.isAnnotationPresent(Yin.class)) {
                Yin annotation = declaredField.getAnnotation(Yin.class);
                String value = annotation.value();
                declaredField.setAccessible(true);
                try {
                    declaredField.set(bean,value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
